package com.yifi.module.jwflow.controller;

import cn.hutool.core.util.StrUtil;
import com.jfinal.core.Path;
import com.jfinal.json.JFinalJson;
import com.jfinal.json.Json;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.base.BaseController;
import com.yifi.common.plugins.JwFlowPlugin;
import com.yifi.module.jwflow.service.GetGroupUsers;
import io.gitee.yaoshanli.engine.domain.UserModel;
import io.gitee.yaoshanli.engine.model.WfNodeModel;
import io.gitee.yaoshanli.engine.model.WfProcessModel;

import java.util.List;

@Path(value="/jwflow_view",viewPath = "/jwflow/")
public class jwflow_view_controller extends BaseController {

    public void index()
    {
        String task_id=getPara("task_id","");
        String wf_id=Db.queryStr("select wf_id from jw_wf_task where id='"+task_id+"'");
        if(StrUtil.isEmpty(wf_id))
        {
            wf_id=getPara("wf_id");
        }
        String node_id= Db.queryStr("select node_id from jw_wf_task where id='"+task_id+"'");

        set("node_id",node_id );
        set("task_id", task_id);
        set("wf_id", wf_id);

        render("jwflow_view.html");
    }

    public void history()
    {
        String task_id=getPara("task_id","");
        String wf_id=Db.queryStr("select wf_id from jw_wf_task where id='"+task_id+"'");
        if(StrUtil.isEmpty(wf_id))
        {
            wf_id=getPara("wf_id");
        }
        //当前待审批节点
        String node_id= Db.queryStr("select node_id from jw_wf_task where id='"+task_id+"'");

        set("node_id",node_id );
        set("task_id", task_id);
        set("wf_id", wf_id);

        //当前节点信息
        WfNodeModel wfNodeModel= JwFlowPlugin.getEngine().flow().getNode(wf_id,node_id);
        // 流程历史列表
        List<WfProcessModel> hisList= JwFlowPlugin.getEngine().task().getTaskHisList(task_id);
        set("hislist", hisList);

        set("hisNodes", JFinalJson.getJson().toJson(hisList));

        String node_type=wfNodeModel.getShape();
        set("node_type",node_type);
        String next_node_name=wfNodeModel.getName();
        String next_node_id=wfNodeModel.getId();
        String node_group=wfNodeModel.getBy_group();

        if(!node_type.equals("end-node"))
        {
            wfNodeModel= JwFlowPlugin.getEngine().flow().getNextNode(wf_id,node_id);
            next_node_name=wfNodeModel.getName();
            next_node_id=wfNodeModel.getId();
            node_group=wfNodeModel.getBy_group();
        }
        set("node_name",next_node_name);

        node_group=StrUtil.isNotEmpty(node_group)?node_group:"0";
        //获取下一个节点配置的人员组
        String users="";
        GetGroupUsers getGroupUsers=new GetGroupUsers();
        getGroupUsers.setGroup(node_group);
        List<UserModel> listUser = getGroupUsers.getUsers();
        if(listUser.size()>0)
        {
            for (UserModel u:listUser) {
                users+=u.getText()+";";
            }
        }
        else
        {
            //如果人员组中不存在人，那么节点下一步处理人还是上一个节点的待处理人
            List<Record> list = Db.find("select user_id from jw_wf_task_handler where task_id='"+task_id+"'");
            for (Record record:list) {
                users+=record.getStr("user_id")+";";
            }
        }
        set("users",users);

        render("jwflow_history.html");
    }
}
